<template>
  <div v-loading="loading" class="app-container">
    <div class="filter-container">
      <el-input v-model="searchObj.data.name" style="width: 200px;" size="medium" placeholder="资源名称"/>
      <el-input v-model="searchObj.data.parent" style="width: 200px;" size="medium" placeholder="父节点名称"/>
      <el-button type="primary" size="medium" icon="el-icon-search" @click="getSearchTree()">搜索</el-button>
      <el-button type="primary" size="medium" @click="getTemplateTree()">重置</el-button>
      <span class="filter-container-right">
        <el-button v-permission= "{ permissionCode: 'resource:add' }" type="primary" size="medium" icon="el-icon-plus" @click="addTemplate">新增</el-button>
      </span>
    </div>
    <tree-table :data="hardNodeTree" :columns="columns" :expand-all="true">
      <el-table-column label="是否公开" min-width="80">
        <template slot-scope="scope">
          {{ scope.row.is_public === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <!--<el-table-column label="创建日期" min-width="160" prop="create_date"/>-->
      <!--<el-table-column label="修改日期" min-width="160" prop="modify_date"/>-->
      <el-table-column label="操作" min-width="280">
        <template slot-scope="scope">
          <el-button v-permission= "{ permissionCode: 'resource:update' }" type="primary" size="mini" icon="el-icon-edit" @click="editResource(scope.row)">编辑</el-button>
          <router-link v-permission= "{ permissionCode: 'resource:permission' }" :to="{ name: 'PermissionList', params: { resource_id: scope.row.id } }">
            <el-button type="success" size="mini"><svg-icon icon-class="permission" class="permission-icon"/>权限</el-button>
          </router-link>
          <el-button v-permission= "{ permissionCode: 'resource:delete' }" type="danger" size="mini" icon="el-icon-delete" @click="delTreeNode(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>
    <el-dialog :title=" isEdit ? '编辑资源' : '新增资源'" :visible.sync="dialogFormVisible" width="600px">
      <el-form ref="ruleForm" :model="nodeForm" :rules="rules" >
        <el-form-item :label-width="formLabelWidth" label="父资源ID">
          <el-input v-model="nodeForm.parentId" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="资源名称" prop="name">
          <el-input v-model="nodeForm.name" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="标识符" prop="code">
          <el-input v-model="nodeForm.code" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="权重" prop="priority">
          <el-input v-model="nodeForm.priority" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="资源URL" prop="url">
          <el-input v-model="nodeForm.url" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否公开">
          <el-switch v-model="nodeForm.is_public" :active-value="1" :inactive-value="0" active-color="#13ce66"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述">
          <el-input v-model="nodeForm.description" type="textarea" auto-complete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="isEdit ? confirmEditResource('ruleForm') : confirmAddTemplate('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import treeTable from '@/components/TreeTable'
import commonApi from '@/api/commonApi'
export default {
  name: 'Resource',
  components: { treeTable },
  filters: {
    typeFilter(val, data) {
      const typeMap = {}
      for (let i = 0; i < data.length; i++) {
        typeMap[data[i].value] = data[i].label
      }
      return typeMap[val]
    }
  },
  data() {
    return {
      msg: '',
      dialogFormVisible: false,
      loading: false,
      isEdit: false,
      nodeForm: {
        name: '',
        code: '',
        priority: '',
        url: '',
        is_public: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入资源名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        priority: [
          { required: true, message: '请输入权重', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入URL', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      columns: [
        {
          text: '资源名称',
          value: 'text',
          width: 200
        },
        {
          text: '资源URL',
          value: 'href',
          width: 200
        },
        {
          text: '标识符',
          value: 'code',
          width: 200
        },
        {
          text: '资源描述',
          value: 'description',
          width: 150
        },
        {
          text: '资源ID',
          value: 'id',
          width: 80
        },
        {
          text: '权重',
          value: 'priority',
          width: 80
        }
      ],
      hardNodeTree: [],
      searchObj: {
        currentNo: 1,
        pageSize: 100,
        data: {
          name: '',
          parent: ''
        }
      }
    }
  },
  created() {
    this.getTemplateTree()
  },
  methods: {
    getTemplateTree() {
      this.loading = true
      commonApi('/resource/treeview', 'get').then(response => {
        if (response.code === 1) {
          this.hardNodeTree = response.data
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },
    getSearchTree() {
      this.loading = true
      commonApi('/resource/queryResource', 'post', this.searchObj).then(response => {
        if (response.code === 1) {
          this.hardNodeTree = response.data.rows
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },
    delTreeNode(row) {
      this.$confirm(`确定要删除资源：${row.text} ！`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        commonApi('/resource/deleteResource', 'post', [row.id]).then(response => {
          if (response.code === 1) {
            this.getTemplateTree()
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {
        /* this.$message({
          type: 'info',
          message: '已取消删除'
        })*/
      })
    },
    addTemplate() {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    confirmAddTemplate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          this.loading = true
          commonApi('/resource/addResource', 'post', this.nodeForm).then(response => {
            if (response.code === 1) {
              this.nodeForm = {}
              this.loading = false
              this.getTemplateTree()
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel() {
      this.dialogFormVisible = false
      this.nodeForm = { is_public: 0 }
    },
    editResource(row = {}) {
      console.log(row)
      this.dialogFormVisible = true
      this.isEdit = true
      this.nodeForm = {
        name: row.text,
        code: row.code,
        priority: row.priority,
        description: row.description,
        url: row.href,
        id: row.id,
        parentId: row.parentId,
        is_public: row.is_public
      }
    },
    confirmEditResource(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          this.loading = true
          commonApi('/resource/updateResource', 'post', this.nodeForm).then(response => {
            if (response.code === 1) {
              this.nodeForm = {}
              this.loading = false
              this.getTemplateTree()
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .permission-icon{
    margin-right: 5px;
  }
</style>
